package cn.fizzo.hub.sdk;

import android.content.Context;
import android.content.Intent;

import java.util.List;

import cn.fizzo.hub.sdk.entity.AntPlusInfo;
import cn.fizzo.hub.sdk.observer.NotifyNewAntsListener;
import cn.fizzo.hub.sdk.service.SerialPortService;
import cn.fizzo.hub.sdk.subject.NotifyNewAntsSubjectImp;
import cn.fizzo.hub.sdk.utils.ExceptionU;

/**
 * Created by Raul.Fan on 2018/1/12.
 */
public class Fh {

    private static final String TAG = "Fw";

    private static Fh instance;//唯一实例

    private Context mContext;

    private boolean init = false;
    private static boolean debug;//是否打印日志

    private String mConnectMac;//连接的地址

    private NotifyNewAntsSubjectImp mNotifyNewAntsSub;//收到新的Ants的订阅管理

    private Fh() {
    }

    /**
     * 获取堆栈管理的单一实例
     */
    public static Fh getManager() {
        if (instance == null) {
            instance = new Fh();
        }
        return instance;
    }

    /**
     * 初始化
     *
     * @param context
     * @return
     */
    public boolean init(Context context) {
        if (context == null) {
            ExceptionU.ThrowInitNullPointException();
        }
        mContext = context;
        init = true;
        mNotifyNewAntsSub = new NotifyNewAntsSubjectImp();

        Intent AntServiceIntent = new Intent(context,SerialPortService.class);
        context.startService(AntServiceIntent);
        return true;
    }


    /**
     * 查看是否打印debug信息
     * @return
     */
    public boolean isDebug() {
        return debug;
    }

    /**
     * 设置是否打印debug信息
     * @param debug
     */
    public void setDebug(boolean debug) {
        Fh.debug = debug;
    }


    /**
     * 通知收到新的Ant数据
     * @param ants
     */
    public void notifyNewAnts(final List<AntPlusInfo> ants){
        mNotifyNewAntsSub.notify(ants);
    }

    /**
     * 订阅者注册新的Ant数据通知
     * @param observer
     */
    public void registerNotifyNewAntsListener(NotifyNewAntsListener observer){
        mNotifyNewAntsSub.attach(observer);
    }

    /**
     * 订阅者注销新的Ant数据通知
     * @param observer
     */
    public void unRegisterNotifyNewAntsListener(NotifyNewAntsListener observer){
        mNotifyNewAntsSub.detach(observer);
    }
}
